Ext.namespace("NewCustomer");

NewCustomer.Task = function(p) {
	this.parentWin=p;	
	this.userSelector = null;	
	this.winMsg=null;
	
	this.txtRemind=new Ext.form.Hidden({name:'remind'});
	this.hdSuperviseId = new Ext.form.Hidden({name:'superviseId'});
	this.txtSuperviseTitle = new Ext.form.TextField({
		fieldLabel: '问题标题',
		name:'superviseTitle',		
		maxLength:100,
		width: 380
	});
	
	this.txtManagerId=new Ext.form.Hidden({name:'managerId'});
	this.txtManagerName=new Ext.form.Hidden({name:'managerName'});
	this.txtReportDept=new Ext.form.Hidden({name:'reportDept'});
	this.txtReportDeptName = new Ext.form.TextField({
		width: 150,
		fieldLabel: '责任部门',
		name:'reportDeptName',
		disabled: true
	});
	
	this.txtSuperviseContent = new Ext.form.TextArea({
		fieldLabel: '问题描述',
		name:'superviseContent',
		disabled: true,
		height:60,
		width: 380
	});
	
	this.typeStore  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.ISSUE_TYPE2
    });
	this.txtIssueType = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '问题类别',
		name:'issueType',
		store: this.typeStore,
        valueField: 'value',
        displayField: 'name',
        disabled: true,
        mode: 'local',
        triggerAction: 'all'
	});
	//this.txtIssueType.on('select', this.changeType, this);	
	this.dtIssueDate = new Ext.form.DateField({
    	fieldLabel: '提问时间',
    	name:'issueDate',
		width: 150,
		format: 'Y-m-d',
		disabled: true
	});
	this.typeLebel  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.ISSUE_LEVEL2
    });
	this.txtInstancyLevel = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '紧急程度',
		name:'instancyLevel',
		store: this.typeLebel,
		value:"0",	
        valueField: 'value',
        displayField: 'name',
        disabled: true,
        mode: 'local',
        triggerAction: 'all'
	});
	
	this.hdInspectorId = new Ext.form.Hidden({name:'inspectorId'});
	this.txtInspectorName = new Ext.form.TextField({
        fieldLabel: '执行人',
        name:'inspectorName',
        width: 380,
        readOnly: true,
		allowBlank: false,
        blankText: '请选择执行人!',
		listeners: {
		   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt;
				cmp.getEl().on('click', function(){ self.showUser();}, self); 
			}
		}
    });
	
	this.txtNotion = new Ext.form.TextArea({
		fieldLabel: '处理意见',
		name:'notion',
		width: 380,
		height: 55,
		maxLength:1000
	});
	this.txtCompleteDate =  new Ext.form.DateField({
    	fieldLabel: '完成时限',
    	name:'completeDate',
    	width:150,
    	readOnly:true,
    	allowBlank: false,
        blankText: '请选择完成时限!',
		format: 'Y-m-d'
	});
	this.txtCreateName = new Ext.form.TextField({
		fieldLabel: '填报人',
		name:'createName',
		width: 150,
		disabled:true
	});
	this.txtDeptName = new Ext.form.TextField({
		fieldLabel: '营业厅',
		name:'deptName',
		width: 150,
		disabled:true
	});
	this.files=new Ext.Container({ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: []
	    });
	this.form = new Ext.FormPanel({
		bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 70,
        labelAlign: 'right',
        items: [ { layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .50, items: [this.txtIssueType]},
	        	         { columnWidth: .50, items: [this.txtReportDeptName]}]
	    				},{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .50, items: [this.dtIssueDate]},
	        	         { columnWidth: .50, items: [this.txtInstancyLevel]}]
	    	},{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .50, items: [this.txtCreateName]},
	        	         { columnWidth: .50, items: [this.txtDeptName]}]
	    	},this.txtSuperviseContent,this.txtNotion,this.txtInspectorName,this.txtCompleteDate,
	    	this.txtManagerId,this.txtManagerName,this.txtReportDept,
	    	this.hdInspectorId,this.hdSuperviseId,this.txtRemind,this.files
	    	]
    });    
    this.btnTask = new Ext.Button({
    	text:'派发',
    	handler:this.onTask,
    	scope: this
    });
	NewCustomer.Task.superclass.constructor.call(this, {
		title: '厅问题派发任务',
        autoHeight: false,
        width: 500,
        y: 20,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form],
        buttons: [this.btnTask,{
            text: '关闭',
            scope: this,
            handler: function() {
	        	this.hide();
        	}
        }],
        buttonAlign:'center'
	});
};
Ext.extend(NewCustomer.Task, Ext.Window, {
	initWin:function(data){
		//alert(Ext.util.JSON.encode(data));
		this.form.getForm().setValues(data);
		this.userSelector==null;
		this.initFile(data);	
	},
	initFile:function(o){
		this.files.removeAll(true);
		var conn = new Ext.data.Connection({
			url:'../superviseIssue/searchUpLoadFiles.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		})
		Utils.request(conn,{
			params:{superviseId:o.superviseId},
			scope:this,
			success:function(data){
				var files=data.result;
				for(var i=0;i<files.length;i++){
					this.scope.addFile(files[i]);
				}
				this.scope.files.doLayout();
			}
		});
	},
	addFile:function(o){
		var href='../superviseIssue/downloadFile.form?fileId='+o.ACCESSORY_ID+'&timesnap=' + Date.parse(new Date());
		var lbLink=new Ext.form.Label({
			html:'<a href="'+href+'">'+o.ACCESSORY_NAME+'</>',
			width:260
		});	    	
		this.files.add(lbLink);	    
	},
	onTask: function() {
		if(!this.form.getForm().isValid()){
			Utils.info2("有必填项没有填写，请检查！",180);
			return;
		}
		scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.txtRemind.setValue(scope.winMsg.txtRemind);
				scope.saveTask();
			},'你确实要派发任务吗？');
		this.winMsg.show();
		
	},
	saveTask: function() {		
		var conn = new Ext.data.Connection({
  			url:'../superviseIssue/dispatchTask.form?timesnap=' + Date.parse(new Date()),
  			method:'post'
  		})
  		var param=this.form.getForm().getFieldValues();
  		Utils.formatDate(param,'issueDate');  		
  		Utils.formatDate(param,'completeDate');	
  		param['superviseTitle']='厅问题督办：'+this.txtDeptName.getValue()+'-'+this.txtIssueType.getRawValue();
  		param['issueTypeName']=this.txtIssueType.getRawValue();
  		param['instancyLevelName']=this.txtInstancyLevel.getRawValue();
  		//alert(Ext.util.JSON.encode(param));
		Utils.request(conn,{
  			params:param,
  			scope:this,
  			success:function(data){
  				Utils.info2();
				this.scope.hide();
				this.scope.parentWin.queryIssue();
  			}
  		});
	},
	showUser:function(){
		var scope=this;
		if(this.userSelector==null){
			var value=this.hdInspectorId.getValue();
			this.userSelector=new Tree.User(value,function(o){
				scope.setUsers(o);
			},null,'113');
		}
		this.userSelector.show();
	},
	setUsers:function(o){
		var names = [];
		var ids =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			ids.push(o[i].id);
		}
		this.hdInspectorId.setValue(ids.join(','));
		this.txtInspectorName.setValue(names.join(','));	
	}
});

